#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#include <errno.h>
#include <time.h>
// 每隔一秒钟显示一个颜色

#define BLACK 0x00000000  // 黑色
#define RED 0x00ff0000    // 红色
#define GREEN 0x0000ff00  // 绿色
#define YELLOW 0x00ffff00 // 黄色
#define BLUE 0x000000ff   // 蓝色
#define PURPLE 0x00ff00ff // 紫色
#define CYAN 0x0000ffff   // 青色
#define WHITE 0x00ffffff  // 白色
#define GRAY 0x00808080   // 灰色
#define PINK 0x00ff8080   // 粉色
#define ORANGE 0x00ff8000 // 橙色

char *color1[] = {"BLACK", "RED", "GREEN", "YELLOW", "BLUE", "PURPLE", "CYAN", "WHITE", "GRAY", "PINK", "ORANGE"};
int color[] = {BLACK, RED, GREEN, YELLOW, BLUE, PURPLE, CYAN, WHITE, GRAY, PINK, ORANGE};

int main()
{

    srand(time(NULL));
    int fd;
    fd = open("/dev/fb0", O_RDWR);
    if (fd < 0)
    {
        printf("open /dev/fb0 failed error: %s\n", strerror(errno));
        return -1;
    }

    printf("open /dev/fb0 success\n");

    int buf[480][800] = {0};
    int k = 0;
    while (1)
    {
        for (int i = 0; i < 480; i++)
        {
            for (int j = 0; j < 800; j++)
            {
                buf[i][j] = color[k];
            }
        }
        // k = (k + 1) % 4;
        k = (rand() + 1) % 11;

        sleep(1);
        lseek(fd, 0, SEEK_SET);
        int res = write(fd, buf, sizeof(buf));
        printf("write %d bytes, color is %s\n", res, color1[k]);
    }
    close(fd);
}